package com.fare.taluo.core.domain.config.line.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntityRepository;
import com.fare.taluo.util.StringUtils;

@Repository
public class FlightInfoEntityRepositoryImpl extends EntityRepositoryHibernate implements FlightInfoEntityRepository{

	@Override
	public List<FlightInfoEntity> findByLine(String fromCityCode,
			String toCityCode) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from FlightInfoEntity flight where 1=1 " ;
		if(StringUtils.isNotEmpty(fromCityCode)){
			hql += " and flight.fromCity= ?";
			params.add(fromCityCode);
		}
		if(StringUtils.isNotEmpty(toCityCode)){
			hql += " and flight.toCity= ?";
			params.add(toCityCode);
		}
		List<FlightInfoEntity> list = super.find(hql,params.toArray(),FlightInfoEntity.class);
		return list;
	}

	@Override
	public List<FlightInfoEntity> findByFlightNo(String flightNo) {
		List<Object> params = new ArrayList<Object>();
		String hql="from FlightInfoEntity flight where 1=1 ";
		if(StringUtils.isNotEmpty(flightNo)){
			hql+=" and flight.flightNo like '"+flightNo+"%'";
		}
		List<FlightInfoEntity> list=super.find(hql,params.toArray(),FlightInfoEntity.class);
		return list;
	}
	public List<FlightInfoEntity> findByLine(String fromCityCode,String toCityCode,String air_key){
		List<Object> params = new ArrayList<Object>();
		String hql = "from FlightInfoEntity flight where 1=1 " ;
		if(StringUtils.isNotEmpty(fromCityCode) && !fromCityCode.equals("")){
			hql += " and flight.fromCity= ?";
			params.add(fromCityCode);
		}
		if(StringUtils.isNotEmpty(toCityCode) && !toCityCode.equals("")){
			hql += " and flight.toCity= ?";
			params.add(toCityCode);
		}if(StringUtils.isNotEmpty(air_key) && !air_key.equals("")){
			hql += " and flight.companyCode= ? order by flight.flightStartTime";
			params.add(air_key);
		}
		List<FlightInfoEntity> list = super.find(hql,params.toArray(),FlightInfoEntity.class);
		return list;
	}
	@Override
	public List<FlightInfoEntity> findByLine(List<PortEntity> fromCityCode,List<PortEntity>  toCityCode,String air_key){
		List<Object> params = new ArrayList<Object>();
		String hql = "from FlightInfoEntity flight where 1=1 " ;
		if(fromCityCode!=null&&fromCityCode.size()!=0){
			hql += " and (";
			for(PortEntity port:fromCityCode){
				params.add(port.getCode());
				hql += " flight.fromCity= ?";
				hql += " or";
			}
			hql = StringUtils.removeEnd(hql, "or");
			hql += ")";
		}
		if(toCityCode!=null&&toCityCode.size()!=0){
			hql += " and (";
			for(PortEntity port:toCityCode){
				params.add(port.getCode());
				hql += " flight.toCity= ?";
				hql += " or";
			}
			hql = StringUtils.removeEnd(hql, "or");
			hql += ")";
		}if(StringUtils.isNotEmpty(air_key)){
			hql += " and flight.companyCode= ? order by flight.flightStartTime";
			params.add(air_key);
		}
		List<FlightInfoEntity> list = super.find(hql,params.toArray(),FlightInfoEntity.class);
		return list;
	}
	public List<FlightInfoEntity> findByLineCodeNotIn (String fromCityCode,String toCityCode,String companyCodeNotIn){
		List<Object> params = new ArrayList<Object>();
		String hql = "from FlightInfoEntity flight where 1=1 " ;
		if(StringUtils.isNotEmpty(fromCityCode)){
			hql += " and flight.fromCity= ?";
			params.add(fromCityCode);
		}
		if(StringUtils.isNotEmpty(toCityCode)){
			hql += " and flight.toCity= ?";
			params.add(toCityCode);
		}if(StringUtils.isNotEmpty(companyCodeNotIn)){
			hql += " and flight.companyCode<> ?";
			params.add(companyCodeNotIn);
		}
		List<FlightInfoEntity> list = super.find(hql,params.toArray(),FlightInfoEntity.class);
		return list;
	}
	public List<FlightInfoEntity> findByLineAndDate(String fromCityCode,String toCityCode,String date){
		List<Object> params = new ArrayList<Object>();
		String hql = "from FlightInfoEntity flight where 1=1 " ;
		if(StringUtils.isNotEmpty(fromCityCode)){
			hql += " and flight.fromCity= ?";
			params.add(fromCityCode);
		}
		if(StringUtils.isNotEmpty(toCityCode)){
			hql += " and flight.toCity= ?";
			params.add(toCityCode);
		}if(StringUtils.isNotEmpty(date)){
			hql += " and flight.flightStartTime";
			params.add(date);
		}
		List<FlightInfoEntity> list = super.find(hql,params.toArray(),FlightInfoEntity.class);
		return list;
	}

	@Override
	public void save(FlightInfoEntity entity) {
		 super.save(entity);
	}
	@Transactional
	public void tranSave(FlightInfoEntity entity){
		this.save(entity);
	}

	@Override
	public List<FlightInfoEntity> findByConditions(String companyCode,
			String fromCity, String toCity,String flightNo, Date entryTime) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from FlightInfoEntity flight where 1=1 " ;
		if(StringUtils.isNotEmpty(companyCode)){
			hql += " and flight.companyCode= ?";
			params.add(companyCode);
		}
		if(StringUtils.isNotEmpty(fromCity)){
			hql += " and flight.fromCity= ?";
			params.add(fromCity);
		}
		if(StringUtils.isNotEmpty(toCity)){
			hql += " and flight.toCity= ?";
			params.add(toCity);
		}if(null!=entryTime){
			hql += " and flight.entryTime >= ?  and flight.entryTime <= ? ";
			params.add(entryTime);
			Calendar cal = Calendar.getInstance();
			cal.setTime(entryTime);
			cal.add(Calendar.DATE, 1);
			params.add(cal.getTime());
		}if(StringUtils.isNotEmpty(flightNo)){
			hql += " and flight.flightNo like ?";
			params.add('%'+flightNo+'%');
		}
		List<FlightInfoEntity> list = super.find(hql,params.toArray(),FlightInfoEntity.class);
		return list;
	}

	@Override
	public List<FlightInfoEntity> findLineByCodes(String fromCityCode, String toCityCode, String carrier_key) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from FlightInfoEntity flight where 1=1 " ;
		if(StringUtils.isNotEmpty(fromCityCode)){
			hql += " and flight.fromCity= ?";
			params.add(fromCityCode);
		}
		if(StringUtils.isNotEmpty(toCityCode)){
			hql += " and flight.toCity= ?";
			params.add(toCityCode);
		}if(StringUtils.isNotEmpty(carrier_key)){
			hql += "  and flight.companyCode= ?";
			params.add(carrier_key);
		}
		hql+=" order by flight.flightStartTime";
		List<FlightInfoEntity> list = super.find(hql,params.toArray(),FlightInfoEntity.class);
		return list;
	}
}
